#include "dxinput.h"


DXinput::DXinput(HINSTANCE hInstance,HWND hWnd)
{
	_hInstance = hInstance;
	_hWnd = hWnd;
}

DXinput::~DXinput(void)
{
	if (_dikeyboard != NULL)
	{
		_dikeyboard->Unacquire();
		_dikeyboard->Release();
		_dikeyboard = NULL;
	}
}

int DXinput::InitDirectInput()
{
	HRESULT result = DirectInput8Create(
		_hInstance,
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		(VOID**)&_dinput,
		NULL);

	if (result != DI_OK)
	{
		MessageBox(NULL,"Error1","Error",MB_OK);
		return 0;
	}
	result = _dinput->CreateDevice(GUID_SysKeyboard,&this->_dikeyboard,NULL);

	if (result != DI_OK)
	{
		MessageBox(NULL,"Error2","Error",MB_OK);
		return 0;
	}

	return 1;
}

int DXinput::InitKeyboard()
{
	HRESULT result;
	
	result = _dikeyboard->SetDataFormat(&c_dfDIKeyboard);

	if (result != DI_OK)
		return 0;

	result = _dikeyboard->SetCooperativeLevel(_hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
	
	if (result != DI_OK)
		return 0;

	result = _dikeyboard->Acquire();

	if (result != DI_OK)
		return 0;

	return 1;
}

void DXinput::PollKeyboard()
{
	if (_dikeyboard->Poll() != DI_OK)
		_dikeyboard->Acquire();
	_dikeyboard->GetDeviceState(sizeof(keys),(LPVOID)&keys);
	int i=0;
}

int DXinput::isKeyPress(int KeyCode)
{
	return (keys[KeyCode] & 0x80);
}